package com.atmc.service.impl;

import com.atmc.entity.LoginUser;
import com.atmc.entity.ResponseResult;
import com.atmc.entity.User;
import com.atmc.entity.UserPotrait;
import com.atmc.mapper.ArticleMapper;
import com.atmc.mapper.HeadPotraitMapper;
import com.atmc.service.ArtLikerService;
import com.atmc.service.LoginService;
import com.atmc.service.UserService;
import com.atmc.utils.JwtUtil;
import com.atmc.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    AuthenticationManager authenticationManager;
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    @Qualifier("redisTemplate1")
    RedisTemplate redisTemplate;
    @Autowired
    UserService userService;
    @Autowired
    ArtLikerService artLikerService;
    @Autowired
    ArticleMapper artitleMapper;
    @Override
    public ResponseResult login(User user) {

        //3使用ProviderManager auth方法进行验证
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(),user.getPassword());
        Authentication authenticate = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        //校验失败了
        if(Objects.isNull(authenticate)){
            throw new RuntimeException("用户名或密码错误！");
//            return new ResponseResult(404,"账号密码错误");
        }else {

            //4自己生成jwt给前端
            Object principal = authenticate.getPrincipal();
            LoginUser loginUser = (LoginUser) principal;
            String userId = loginUser.getUser().getUserId().toString();
            User user1 = loginUser.getUser();
            String userName = user1.getUserName();

            List<Long> longs = artLikerService.selectIdByName(userName);
            //用户所有返回的数据
            UserPotrait userPotrait = userService.selectOneByName(userName);
            String jwt = JwtUtil.createJWT(userId);
            Map<String, Object> map = new HashMap();
            map.put("token", jwt);
            map.put("userDatas",userPotrait);
//            for (Long aLong : longs) {
//                Long aLong1 = artLikerService.queryByIdAndName(aLong, userName);
//                if (aLong1 ==null){
//
//                }
//            }

            map.put("articleId",longs);
            //5系统用户相关所有信息放入redis
            redisTemplate.opsForValue().set("userId:"+userId,loginUser);

            return new ResponseResult(200, "登陆成功", map);
        }
    }

    @Override
    public ResponseResult outLogin() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Long userId = loginUser.getUser().getUserId();

       redisUtils.del("userId:"+userId);
        return new ResponseResult(200,"退出成功！");
    }
}